package com.xmut.controller;


import com.xmut.pojo.Result;
import com.xmut.pojo.User;
import com.xmut.pojo.Voluntary;
import com.xmut.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin("*")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAllUser")
    public Result getAllUser()
    {
        List<User> users = userService.getAllUser();
        return Result.success(users);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user)
    {
        System.out.println(user.getIsadmin());
        Boolean isSuccess = userService.rigister(user);
        if(isSuccess)
        {
            return Result.success();
        }
        else
        {
            return Result.error("注册失败");
        }

    }
    @PostMapping("/loginVer")
    public Result login(@RequestBody User user)
    {

        Boolean isSuccess = userService.login(user);

        if(isSuccess)
        {
            User user1 = userService.getUserByName(user);
            return Result.success(user1);
        }
        else
        {
            return Result.error("登录失败！");
        }

    }
    @GetMapping("/getVolunteerInfo/{id}")
    public Result getVolunteerInfo(@PathVariable Integer id)
    {
        User user = userService.getVolunteerInfo(id);
        return Result.success(user);
    }

    @PostMapping("/insVolunteerAdd")
    public Result insVolunteerAddress(@RequestBody User user)
    {
        userService.insVolunteerAddress(user);

        return Result.success();
    }

    @GetMapping("/setVolunteer/{id}")
    public Result setVolunteer(@PathVariable Integer id)
    {
        userService.setVolunteer(id);
        return Result.success();
    }

}
